<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class PropEvent extends Base
{
    protected $table = 'prop_event';
    protected $primaryKey = 'prop_event_id';

    protected $redisKeyProEvent = 'CACHE:PROP:EVENT:';
    protected $fields = ["prop_event_id", "prop_id", "name", "data_id", "used_at", "data_type", "type"];

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 道具-列表分页
     *
     * @param array $condition 条件
     * @param int $page 页数 默认0
     * @param int $limit 条数 默认10
     * @return array
     * */
    public function getPropEventList($condition = [], $page = 0, $limit = 10)
    {
        $db = DB::table($this->table)->where($condition);

        // 总条数
        $totalCount = $db->count($this->primaryKey);

        // 查询数据
        $list = $db->offset($page * $limit)->limit($limit)->get()->toArray();
        $list = self::objToArray($list);

        foreach ($list as $key => $value) {
            if (!empty($value['data_image'])) {
                $list[$key]['data_image'] = build_image($value['data_image']);
            }
        }

        return $this->checkPageData($page, $limit, $totalCount, $list);
    }

    /**
     * 道具-根据道具ID获取
     *
     * @param array $propIdArray 道具ID组
     * @return array
     * */
    public function getPropEventByPropIds($propIdArray)
    {
        $data = [];

        do {
            $getData = Redis::MGET(array_map(function ($propEventId) {
                return $this->redisKeyProEvent . $propEventId;
            }, $propIdArray));

            foreach ($getData as $key => $value) {
                if (empty($value)) {
                    unset($getData[$key]);
                    continue;
                }
                $data[] = json_decode($value, true);
                unset($propIdArray[$key]);
            }
            if (empty($propIdArray)) {
                break;
            }

            $db = DB::table($this->table);
            $getData = $db->whereIn("prop_id", $propIdArray)->get()->toArray();
            $getData = self::objToArray($getData);
            foreach ($getData as $key => $value) {
                if (!empty($value['data_image'])) {
                    $value['data_image'] = build_image($value['data_image']);
                }
                $data[] = $value;
                Redis::setex($this->redisKeyProEvent . $value["prop_id"], 3600, json_encode($value, JSON_UNESCAPED_UNICODE));
            }

        } while (false);

        return $data;
    }

    /**
     * 道具-根据道具ID获取
     *
     * @param int $propId 道具ID
     * @return array
     * */
    public function getPropEventByPropId($propId)
    {
        $db = DB::table($this->table);
        $getData = $db->where('prop_id', $propId)->get()->toArray();
        $getData = self::objToArray($getData);

        foreach ($getData as $key => $value) {
            if (!empty($value['image'])) {
                $getData[$key]['image'] = build_image($value['image']);
            }
        }

        return $getData;
    }

    /**
     * 道具-创建/更新/删除
     *
     * @param array $data 数据
     * @param string $type 操作类型 默认 insert=创建，update=更新
     * @param int $propEventId 道具ID
     * @return bool
     * */
    public function CUDPropConfig($data = [], $type = 'insert', $propEventId = 0)
    {
        switch ($type) {
            case 'insert':
                $data['created_at'] = time();
                $ret = DB::table($this->table)->insertGetId($data);
                $propEventId = $ret;
                break;
            case 'update':
                $ret = DB::table($this->table)->where([$this->primaryKey => $propEventId])->update($data);
                break;
            case 'delete':
                $ret = DB::table($this->table)->where([$this->primaryKey => $propEventId])->delete();
                break;
            default:
                $ret = false;
                break;
        }

        if ($ret and $propEventId > 0 and Redis::exists($this->redisKeyProEvent . $propEventId)) {
            Redis::DEL($this->redisKeyProEvent . $propEventId);
        }

        return $ret;
    }



}